// pages/region/region.js
var usedata = require("../regiondata/regiondata.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressImg: '',
    address: '',
    address2:[],
    fondBtn:true,
    noText:true,
    noClear:true,
    inputValue: null,
    textInput:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      addressImg: usedata.addressImg,
      address: usedata.address
    })
  },
  slide:function(e){
    if (this.data.fondBtn){
      let index = e.currentTarget.dataset.index;
      var address = this.data.address;
      address[index].slide = !address[index].slide;
      this.setData({
        address: address
      }) 
    }
   
  },
  goIndex: function () {//跳转到首页
    wx.switchTab({
      url: '../index/index'
    });
  },
  textInput: function (e) {//获取输入框内容
    var address2 = this.data.address2 = [];
    if (e.detail.value!==''){
      var address = this.data.address;
      this.setData({
        textInput: e.detail.value
      });
      this.search(e.detail.value);
      this.setData({
        noClear: false
      })
    }
    else{
      address2.length = 0;
      this.setData({
        address2: address2,
        noText: true,
        fondBtn: true,
        noClear: true,
        'inputValue': '',
      })
    }
  },
  search:function(a){//搜索
    var address = this.data.address;
    var address2 = this.data.address2=[];
    var address3 = [];
    for (let i = 0; i < address.length; i++) {
      for (let j = 0; j < address[i].addressText.length; j++) {
        if (address[i].addressText[j].name.match(a)) {
          address2.push(address[i])
          this.setData({
            address2: address2,
            noText: true,
            fondBtn: false
          })
         } else if (address2.length==0){
          address2.length = 0;
          this.setData({
            address2: address2,
            noText:false,
            fondBtn: false
          })

        }
      }
    }    
  },
  // clearInputEvent: function (res) {
  //   this.setData({
  //     noClear: true,
  //     'inputValue': '',
  //   })
  // },
  // clear: function () {
  //   this.setData({
  //     noText: true,
  //     fondBtn: true
  //   })
  // },
})